import unittest
import requests
from api.regloginapi import RegLoginApi
from api.recharge_api import RechargeApi
from parameterized import parameterized
from common.read_json import read_json
from common.utils import common_assert
from common.html_uilt import html_util


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 实例化登录模块封装的类
        self.reg_login_api = RegLoginApi(self.session)
        # 实例化充值模块封装的类
        self.rechargeapi = RechargeApi(self.session)
        # 登录
        response_l = self.reg_login_api.user_login("15878945622", "l123456")

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 获取充值验证码测试用例
    @parameterized.expand(read_json('/recharge_data','img_verify_code'))
    def test01_get_recharge_verify_code(self,r,exp_status_code):
        # 获取充值图片验证码
        response = self.rechargeapi.get_recharge_verify_code(r)
        # 断言
        self.assertEqual(exp_status_code, response.status_code)

    # 充值测试用例
    @parameterized.expand(read_json('/recharge_data',"rechargeapi"))
    def test02_recharge(self,amount,valicode,exp_status_code,exp_status,exp_description):
        print(f"金额:{amount},验证码:{valicode},预期状态码:{exp_status_code},预期错误码:{exp_status},预期desc:{exp_description}")
        # 获取充值图片验证码
        self.rechargeapi.get_recharge_verify_code(522525552)
        # 充值
        response = self.rechargeapi.recharge( amount, valicode)
        json_data = response.json()
        # 断言
        self.assertEqual(exp_status_code, response.status_code)
        self.assertEqual(exp_status, json_data['status'])
        self.assertIn(exp_description,json_data['description'])

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 获取充值图片验证码
        response = self.rechargeapi.get_recharge_verify_code(52020202)
        # 充值
        response_c = self.rechargeapi.recharge('500000',  '8888')
        # 获取第三方开户接口的url
        url = html_util(response_c)[0]
        # 获取第三方开户接口的请求体数据
        form_data = html_util(response_c)[1]
        # 获取第三方接口返回的响应结果
        response_3 = self.rechargeapi.third_recharge(url, form_data)
        # 断言
        self.assertIn('OK', response_3.text)
